package net.jltec.view;

import java.util.ArrayList;
import java.util.List;

import net.jltec.config.AppConfig;
import net.jltec.view.adapter.ModelListAdapter;
import net.jltec.view.base.JltecBaseUI;
import net.jltec.view.bean.Model;
import net.jltec.view.model.JltecTaskUI;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class JltecMainUI extends JltecBaseUI implements OnItemClickListener{
	
	private ListView modelListView = null;
	private ModelListAdapter modelListAdapter=null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 声明使用自定义标题
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main_title);// 自定义布局赋值
        initParams();
    }
    
    
    public void initParams(){
    	
    	List<Model> models = new ArrayList<Model>();
    	Model model = new Model();
    	model.name="待办审批";
    	model.modelid=AppConfig.TASK_MODE_ID;
    	model.resId=R.drawable.task;
    	model.appid=AppConfig.APPID_SM;
    	
    	models.add(model);
    	
//    	Model model2 = new Model();
//    	model2.name="日程安排";
//    	model2.modelid=AppConfig.DAYS_MODE_ID;
//    	model2.resId=R.drawable.days;
//    	model2.appid=AppConfig.APPID_SM;
//    	
//    	models.add(model2);
    	
    	 
    	
    	modelListAdapter = new ModelListAdapter(this, models);
    	modelListView = (ListView) findViewById(R.id.modelListView);
    	modelListView.setAdapter(modelListAdapter);
    	
    	
    	modelListView.setOnItemClickListener(this);
    }


    @Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    	if(parent==modelListView){
    		Model model = modelListAdapter.getItem(position);
    		int modelid = model.modelid;
    		if(modelid==AppConfig.TASK_MODE_ID){
	    		Intent intent = new Intent(this, JltecTaskUI.class);
	    		intent.putExtra("model", model);
				startActivity(intent);
    		}else{
    			showToast(getString(R.string.model_error));
    		}
			
			
    	}
	}
    
    private long exitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				exit();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void exit(){
		finish();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}